package com.example.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.dto.CommentWithGoodsDTO;
import com.example.demo.dto.CommentWithUserDTO;
import com.example.demo.entity.Comments;
import com.example.demo.service.CommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comments")
public class CommentsController {
    @Autowired
    private CommentsService commentsService;

    // 添加评论
    @PostMapping("/add")
    public ResponseEntity<CommentWithUserDTO> addComment(@RequestBody Comments comment) {
        try {
            CommentWithUserDTO savedComment = commentsService.addComment(comment);
            return ResponseEntity.ok(savedComment);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
    }

    // 获取商品评论
    @GetMapping("/goods/{goodsId}")
    public ResponseEntity<List<CommentWithUserDTO>> getGoodsComments(@PathVariable Integer goodsId) {
        List<CommentWithUserDTO> comments = commentsService.getCommentsByGoods(goodsId);
        return ResponseEntity.ok(comments);
    }

    // 获取用户评论
    @GetMapping("/user/{userId}")
    public ResponseEntity<List<Comments>> getUserComments(@PathVariable Integer userId) {
        List<Comments> comments = commentsService.getCommentsByUser(userId);
        return ResponseEntity.ok(comments);
    }

    // 删除评论
    @DeleteMapping("/delete/{commentId}")
    public ResponseEntity<String> deleteComment(@PathVariable Integer commentId) {
        try {
            String result = commentsService.deleteComment(commentId);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("删除评论失败: " + e.getMessage());
        }
    }

    // 更新评论
    @PutMapping("/update")
    public ResponseEntity<String> updateComment(@RequestBody Comments comment) {
        try {
            String result = commentsService.updateComment(comment);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("更新评论失败: " + e.getMessage());
        }
    }

    @GetMapping("/user/{userId}/page")
    public ResponseEntity<IPage<CommentWithGoodsDTO>> getUserCommentsWithGoods(
            @PathVariable Integer userId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size,
            @RequestParam(required = false) String keyword) {

        Page<CommentWithGoodsDTO> pageParam = new Page<>(page, size);
        IPage<CommentWithGoodsDTO> commentsPage = commentsService.getCommentsWithGoodsByUser(userId, pageParam, keyword);
        return ResponseEntity.ok(commentsPage);
    }
}
